package com.xkcyy.one.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xkcyy.one.entity.TrainingExample;
import com.xkcyy.one.exception.BusinessException;
import com.xkcyy.one.mapper.TrainingExampleMapper;
import com.xkcyy.one.mapper.TrainingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 训练样例服务类
 */
@Service
public class TrainingExampleService {
    
    private final TrainingExampleMapper trainingExampleMapper;
    private final TrainingMapper trainingMapper;
    
    @Autowired
    public TrainingExampleService(TrainingExampleMapper trainingExampleMapper, TrainingMapper trainingMapper) {
        this.trainingExampleMapper = trainingExampleMapper;
        this.trainingMapper = trainingMapper;
    }
    
    /**
     * 根据训练ID获取样例列表
     *
     * @param trainingId 训练ID
     * @return 样例列表
     */
    public List<TrainingExample> getExamplesByTrainingId(Long trainingId) {
        // 检查训练是否存在
        if (trainingMapper.selectById(trainingId) == null) {
            throw new BusinessException("训练不存在，ID: " + trainingId);
        }
        
        LambdaQueryWrapper<TrainingExample> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainingExample::getTrainingId, trainingId);
        return trainingExampleMapper.selectList(queryWrapper);
    }
    
    /**
     * 根据ID获取样例
     *
     * @param id 样例ID
     * @return 样例
     */
    public TrainingExample getExampleById(Long id) {
        TrainingExample example = trainingExampleMapper.selectById(id);
        if (example == null) {
            throw new BusinessException("训练样例不存在，ID: " + id);
        }
        return example;
    }
    
    /**
     * 创建样例
     *
     * @param example 样例
     * @return 创建后的样例
     */
    @Transactional
    public TrainingExample createExample(TrainingExample example) {
        // 检查训练是否存在
        if (trainingMapper.selectById(example.getTrainingId()) == null) {
            throw new BusinessException("训练不存在，ID: " + example.getTrainingId());
        }
        
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        example.setCreateTime(now);
        example.setUpdateTime(now);
        
        // 插入样例
        trainingExampleMapper.insert(example);
        
        return example;
    }
    
    /**
     * 批量创建样例
     *
     * @param examples 样例列表
     * @return 创建后的样例列表
     */
    @Transactional
    public List<TrainingExample> createExampleBatch(List<TrainingExample> examples) {
        // 检查训练是否存在
        if (!examples.isEmpty() && trainingMapper.selectById(examples.get(0).getTrainingId()) == null) {
            throw new BusinessException("训练不存在，ID: " + examples.get(0).getTrainingId());
        }
        
        // 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        for (TrainingExample example : examples) {
            example.setCreateTime(now);
            example.setUpdateTime(now);
            
            // 插入样例
            trainingExampleMapper.insert(example);
        }
        
        return examples;
    }
    
    /**
     * 更新样例
     *
     * @param example 样例
     * @return 更新后的样例
     */
    public TrainingExample updateExample(TrainingExample example) {
        // 检查样例是否存在
        TrainingExample existingExample = trainingExampleMapper.selectById(example.getId());
        if (existingExample == null) {
            throw new BusinessException("训练样例不存在，ID: " + example.getId());
        }
        
        // 不允许修改训练ID
        example.setTrainingId(existingExample.getTrainingId());
        
        // 设置更新时间
        example.setUpdateTime(LocalDateTime.now());
        
        // 更新样例
        trainingExampleMapper.updateById(example);
        
        return example;
    }
    
    /**
     * 删除样例
     *
     * @param id 样例ID
     */
    public void deleteExample(Long id) {
        // 检查样例是否存在
        TrainingExample existingExample = trainingExampleMapper.selectById(id);
        if (existingExample == null) {
            throw new BusinessException("训练样例不存在，ID: " + id);
        }
        
        // 删除样例
        trainingExampleMapper.deleteById(id);
    }
} 